
import type { GetParams, IpcResponse, KlineFilter, KlineFlat, Klt, ListParams, TradeBot, TradeOrder, TradeOrderFilter, TrendLineResult } from './bindings';
import * as commands from '$lib/commands'
import { DateTime } from 'luxon';

export async function get_trend_line_klines(security_id: string, klt: Klt, size: number): Promise<TrendLineResult | undefined> {
    console.log(`(get_trend_line_klines) 获取K线数据 for security_id=${security_id} for klt=${klt}`);

    // Define the argument
    let args: ListParams<KlineFilter> = {
        page: 0,
        size: size,
        filter: { security_id: security_id, klt: klt },
    }

    const res: IpcResponse<TrendLineResult> = await commands.list_trend_line_klines(args)
    console.log('(get_trend_line_klines) ipcResponse ', res)

    if (res.error) {
        // If the response status is not OK, throw an error
        throw new Error(`(securities/[id]/+page.ts)Server returned an error: ${res.error}`);
    }


    const trendLineResult: TrendLineResult | undefined = res.result?.data;
    return trendLineResult

}


export async function get_kline_flats(security_id: string, klt: Klt, page_num: number, page_size: number): Promise<KlineFlat[] | undefined> {
    console.log(`(Backend.ts::get_kline_flats) 获取K线数据 for security_id=${security_id} for klt=${klt}`);

    // Define the argument
    let args: ListParams<KlineFilter> = {
        page: Number(page_num),
        size: Number(page_size),
        filter: { security_id: security_id, klt: klt },
    }

    const res: IpcResponse<KlineFlat[]> = await commands.list_kline_flats(args)
    console.log('(Backend.ts::get_kline_flats) ipcResponse ', res)

    if (res.error) {
        // If the response status is not OK, throw an error
        throw new Error(`(Backend.ts::get_kline_flats) Server returned an error: ${res.error}`);
    }


    const klineFlatResult: KlineFlat[] | undefined = res.result?.data;
    return klineFlatResult

}


export async function get_trade_orders(trade_bot_id: string, start_kline_id: string | null, end_kline_id: string | null): Promise<TradeOrder[] | undefined> {
    console.log(`(Backend.ts::get_trade_orders) 获取TradeOrder数据 for trade_bot_id: ${trade_bot_id} start_kline_id: ${start_kline_id} end_kline_id: ${end_kline_id}`);
    // Define the argument
    const args: ListParams<TradeOrderFilter> = {
        page: 0,
        size: 100,
        filter: { id: null, trade_bot_id: trade_bot_id, start_kline_id: start_kline_id, end_kline_id: end_kline_id },
    }


    const res: IpcResponse<TradeOrder[]> = await commands.list_trade_orders(args)

    if (res.error) {
        // If the response status is not OK, throw an error
        throw new Error(`(Backend.ts::get_trade_orders) Server returned an error: ${res.error}`);
    }

    const tradeOrders: TradeOrder[] | undefined = res.result?.data;

    return tradeOrders

}




export async function get_trade_bot(trade_bot_id: string): Promise<TradeBot | undefined> {
    console.log(`(Backend.ts::get_trade_orders) 获取 TradeBot 数据 for trade_bot_id=${trade_bot_id}`);

    // Define the argument
    const args: GetParams = {
        id: trade_bot_id
    }


    const res: IpcResponse<TradeBot> = await commands.get_trade_bot(args)

    if (res.error) {
        // If the response status is not OK, throw an error
        throw new Error(`(Backend.ts::get_tradede_bot) Server returned an error: ${res.error}`);
    }

    const tradeBot: TradeBot | undefined = res.result?.data;

    return tradeBot

}